package com.heima.behavior.service.impl;

import com.heima.behavior.dtos.FollowBehaviorDto;
import com.heima.behavior.pojos.ApFollowBehavior;
import com.heima.behavior.service.UserFollow;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.common.ApThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserFollowImpl implements UserFollow {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public ResponseResult setFollow(FollowBehaviorDto dto) {
        if(dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 获取用户id
        ApUser apUser = ApThreadLocalUtil.getApUser();
        if(apUser == null && apUser.getId() == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH,"需要登录才能够进行此操作");
        }
        // 创建关注的key值
        String followKey = "USER_FOLLOW_"+apUser.getId();
        // 创建粉丝的key值
        String fenKey = "USER_FANS_"+dto.getAuthorId();
        // 查询室友已经存在数据
        Boolean followFlag = stringRedisTemplate.opsForHash().hasKey(followKey, dto.getAuthorId().toString());
        if(followFlag && dto.getOperation()==0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "已经关注过");
        }
        if(followFlag && dto.getOperation()==1){
            // 已关注,并且要取消关注
            // 删除redis数据
            stringRedisTemplate.opsForHash().delete(followKey, dto.getAuthorId().toString());
            stringRedisTemplate.opsForHash().delete(fenKey, apUser.getId().toString());
            // 删除数据库数据
            Query query = Query.query(
                    Criteria.where("userId").is(apUser.getId()).and("followId").is(dto.getAuthorId())
            );
            mongoTemplate.remove(query, ApFollowBehavior.class);
        }
        // 没有关注的情况
        if(!followFlag && dto.getOperation()==0){
            //新增到redis
            stringRedisTemplate.opsForHash().put(followKey, dto.getAuthorId().toString(),"1");
            stringRedisTemplate.opsForHash().put(fenKey,apUser.getId().toString(),"1");
            // 新增到数据库
            ApFollowBehavior apFollowBehavior = new ApFollowBehavior();
            apFollowBehavior.setFollowId(dto.getAuthorId());
            apFollowBehavior.setUserId(apUser.getId().longValue());
            apFollowBehavior.setCreatedTime(new Date());
            mongoTemplate.save(apFollowBehavior);
        }
        return ResponseResult.okResult("成功");
    }
}
